package com.jtg.security02.config;

import com.jtg.security02.component.*;
import com.jtg.security02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

/**
 * security配置中心
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    //未登录或toen失效
    @Autowired
    private RestAuthenticationEntryPoint authenticationEntryPoint;

    //用户没有访问权限时的处理器
    @Autowired
    private RestfulAccessDeniedHandler accessDeniedHandler;

    //url动态过虑器
    @Autowired
    private DynamicSecurityFilter dynamicSecurityFilter;

    /**
     * 配置用户验证
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    /**
     * 忽略拦截，不走spring security过虑器
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/js/**");
        super.configure(web);
    }

    /**
     * 登录配置
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //允许访问的资源
                .antMatchers("/admin/login/**").permitAll()
                //其它资源都受保护
                .anyRequest().authenticated()
                // 自定义权限拒绝处理类
                .and()
                .exceptionHandling()
                //没有权限访问时
                .accessDeniedHandler(accessDeniedHandler)
                //未登录或token失效时
                .authenticationEntryPoint(authenticationEntryPoint)
                .and()
                .csrf().disable();
        //自定义url动态过虑器
        http.authorizeRequests()
                .and()
                .addFilterBefore(dynamicSecurityFilter, FilterSecurityInterceptor.class);
    }

    /**
     * 加密模式
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
